package services;

import assemblers.interfaces.PhoneNumberAssembler;
import assemblers.interfaces.PhoneNumberInfoAssembler;
import handlers.PersonServiceHandler;
import infos.PhoneNumberInfo;
import model.Person;
import model.PhoneNumber;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Path("phone")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Stateless
public class PhoneNumberService {

    private PersonServiceHandler personServiceHandler;
    private PhoneNumberAssembler phoneNumberAssembler;
    private PhoneNumberInfoAssembler phoneNumberInfoAssembler;

    @EJB
    public void setPersonServiceHandler(PersonServiceHandler personServiceHandler) {
        this.personServiceHandler = personServiceHandler;
    }

    @EJB
    public void setPhoneNumberAssembler(PhoneNumberAssembler phoneNumberAssembler) {
        this.phoneNumberAssembler = phoneNumberAssembler;
    }

    @EJB
    public void setPhoneNumberInfoAssembler(PhoneNumberInfoAssembler phoneNumberInfoAssembler) {
        this.phoneNumberInfoAssembler = phoneNumberInfoAssembler;
    }

    @PUT
    @Path("{personId}")
    public PhoneNumberInfo addPhoneNumber(@PathParam("personId") Long personId, PhoneNumberInfo numberInfo) {
        PhoneNumber number = phoneNumberAssembler.assemble(numberInfo);
        Person person = personServiceHandler.findPerson(personId);
        PhoneNumber phoneNumber = personServiceHandler.addPhoneNumber(number, person);
        return phoneNumberInfoAssembler.assemble(phoneNumber);
    }

    @Path("{numberId}")
    @DELETE
    public Response removePhoneNumber(@PathParam("numberId") Long numberId) {
        personServiceHandler.removePhoneNumber(numberId);
        return Response.ok().build();
    }
}
